# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=py2exe
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=0.11.0.1
pkgrel=1
pkgdesc='A distutils extension to create standalone Windows programs from Python code (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('MIT')
url='https://github.com/py2exe/py2exe'
depends=(
    ${MINGW_PACKAGE_PREFIX}-python
    ${MINGW_PACKAGE_PREFIX}-python-cachetools
    ${MINGW_PACKAGE_PREFIX}-python-pefile
)
makedepends=(
    ${MINGW_PACKAGE_PREFIX}-python-pip
    ${MINGW_PACKAGE_PREFIX}-python-setuptools
    ${MINGW_PACKAGE_PREFIX}-cc
    ${MINGW_PACKAGE_PREFIX}-python-wheel)
source=(https://github.com/py2exe/${_realname}/archive/v${pkgver}.tar.gz
        001-setup-fix.patch
        002-mingw-fix.patch)
sha256sums=('6bb7ff4834ef2096823710c67edf49c971422775fa41c9c7718c19f0437a2d3d'
            'ca5b791c0bf28f0c60dc77313b300a33fefbbe1da12b64822e47852a256380bf'
            'b98f0451d985cd5afdea91848489b09192228e1dfc58b6e3b354b7d03fff29a1')

prepare() {
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  cd "${srcdir}/python-build-${MSYSTEM}"
  patch -p1 -i ${srcdir}/001-setup-fix.patch
  patch -p1 -i ${srcdir}/002-mingw-fix.patch
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 MIT-License.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-MIT.txt"
  install -Dm644 MPL2-License.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-MPL2.txt"

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    # Remove shebang line
    sed -e '1 { s/^#!.*$// }' -i "${_f}"
  done
}
